package ${package}.controller;

import com.flkin.common.base.Ret;
import ${package}.domain.${className};
import ${package}.service.${className}Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ${author} ${date}
 */
@RestController
@RequestMapping("/api/${changeClassName}")
@Slf4j
public class ${className}Controller {

    @Resource
    private ${className}Service ${changeClassName}Service;

    @GetMapping
    @PreAuthorize("@sec.check('${changeClassName}Read')")
    public ResponseEntity<Object> getAll() {
        return Ret.ok(${changeClassName}Service.getAll());
    }

    @GetMapping("/{id}")
    @PreAuthorize("@sec.check('${changeClassName}Read')")
    public ResponseEntity<Object> findById(@PathVariable Long id) {
        return Ret.ok(${changeClassName}Service.findById(id));
    }

    @PostMapping
    @PreAuthorize("@sec.check('${changeClassName}Create')")
    public ResponseEntity<Object> create(@RequestBody ${className} resource) {
        return Ret.ok(${changeClassName}Service.create(resource));
    }

    @PutMapping
    @PreAuthorize("@sec.check('${changeClassName}Update')")
    public ResponseEntity<Object> update(@RequestBody ${className} resource) {
        ${changeClassName}Service.update(resource);
        return Ret.ok();
    }

    @DeleteMapping
    @PreAuthorize("@sec.check('${changeClassName}Delete')")
    public ResponseEntity<Object> delete(@RequestBody List<Long> ids) {
        ${changeClassName}Service.deleteAll(ids);
        return Ret.ok();
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("@sec.check('${changeClassName}Delete')")
    public ResponseEntity<Object> delete(@PathVariable Long id) {
        ${changeClassName}Service.deleteById(id);
        return Ret.ok();
    }

}
